package com.muyu.body;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @ClassName: JsonRequestHeaderParamsHelper
 * @Author: Zirun.Xin
 * @Date: 2025/3/21 下午4:30
 * @Version: 1.0
 * @Description: JSON请求头参数帮助工具
 */
@Slf4j
public class JsonRequestHeaderParamsHelper {

    public static JSONObject parseHeader(ServletRequest request) throws IOException {
        return JSONObject.parseObject(bufferReaderToString(request.getReader()));
    }

    public static String bufferReaderToString(BufferedReader reader) throws IOException {
        StringBuilder sb = new StringBuilder();
        try  {
            char[] buff = new char[1024];
            int len;
            while ((len = reader.read(buff)) != -1) {
                sb.append(buff, 0, len);
            }
        } catch (IOException e) {
            log.error("bufferReaderToString error", e);
        }
        return sb.toString();
    }
}
